package com.dianshang.linster;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class DxConsumer1 implements MessageListener {

    /**
     * 消息消费者
     */
    // 第二种方法：读取MQ消息
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void onMessage(Message message) {
        // 第二种方法：读取MQ消息
        try {
            JsonNode jsonNode = MAPPER.readTree(message.getBody());
            Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            while(fields.hasNext()){
                Map.Entry<String, JsonNode> current = fields.next();
                String phone = current.getKey();
                String yzm = current.getValue().asText();
                System.out.println(phone+"接受到的验证码是："+yzm);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 第一种方法：读取MQ消息
        byte[] messageBody = message.getBody();
        String msg = new String(messageBody);
        Map map = JSON.parseObject(msg,Map.class); // 将字符串转换为Map
        Set<Map.Entry> set = map.entrySet(); // keySet()：单个对象 entrySet()：多个对象
        for (Map.Entry entry : set) {
            String phone =(String) entry.getKey(); // key
            String yzm =(String) entry.getValue(); // value
            // 发送短信
            System.out.println(phone+"接受到的验证码是："+yzm);
        }
    }

}